package com.muyi.controller.user;

import com.alibaba.fastjson.JSON;
import com.muyi.pojo.entity.User;
import com.muyi.service.UserService;
import com.muyi.service.impl.UserServiceImpl;
import com.muyi.pojo.vo.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(name = "ServletUpdateUser", value = "/updateUser")
public class ServletUpdateUser extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //获取请求参数
        //获取请求参数
        String nickname = request.getParameter("nickname");

        String gender = request.getParameter("gender");
        String contact = request.getParameter("contact");
        Date birthday = null;
        try {
            birthday = sdf.parse(request.getParameter("birthday"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        // float sal = Float.parseFloat(request.getParameter("sal"));
        String career_direction = request.getParameter("career_direction");
        // int dept_id = Integer.parseInt(request.getParameter("dept_id"));
        int user_id = Integer.parseInt(request.getParameter("user_id"));
        Object status = request.getParameter("status");
        String graduation_year = request.getParameter("graduation_year");
        String user_password = request.getParameter("user_password");
        int error_count = Integer.parseInt(request.getParameter("error_count"));
        /*DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime lock_time = null;
        try {
            lock_time = LocalDateTime.parse(request.getParameter("lock_time"), formatter);
        } catch (Exception e) {
            e.printStackTrace();
            lock_time = null; // 或者 LocalDateTime.now() 表示当前时间
        }*/
        //调用service层方法
        User user = new User(user_id, nickname, gender, contact, birthday, career_direction, graduation_year, user_password, status, error_count);
        Result result = null;
        try {
            result = userService.updateUser(user);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //将执行结果返回给前端
        response.getWriter().write(JSON.toJSONString(result));
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
